#include "dailyrecord.h"
#include "ui_dailyrecord.h"

dailyRecord::dailyRecord(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::dailyRecord)
{
    ui->setupUi(this);
    dailyRecordInit();

    recordTimer = new QTimer(this);
    connect(recordTimer,&QTimer::timeout,this,&dailyRecord::ON_recordTimer);
    recordTimer->start(800);

    setLedTimer = new QTimer(this);
    connect(setLedTimer,&QTimer::timeout,this,&dailyRecord::ON_setLedTimer);
    setLedTimer->start(100);

//    setLedTimer_StationStatus = new QTimer(this);
//    connect(setLedTimer_StationStatus,&QTimer::timeout,this,&dailyRecord::ON_setLed_StationStatus_Timer);
//    setLedTimer_StationStatus->start(300);

    threadQueryI=new threadQuery;
    threadQueryItem=new QThread();
    threadQueryI->moveToThread(threadQueryItem);
    threadQueryItem->start();

}

dailyRecord::~dailyRecord()
{
    delete ui;
}

void dailyRecord::dailyRecordInit()
{
    //设置默认显示窗口
    ui->tabWidget->setCurrentIndex(1);

    // 隐藏库位显示界面
    ui->tabWidget->setTabEnabled(2, false);

    //tableWidget 表格初始化
    table_single::GetInstance()->TableWidgetInit(ui->tableWidget,12,"QHeaderView::section{background-color:rgb(211, 215, 212);font:12pt '宋体';color: black;};",30,30);

}

void dailyRecord::setLED(QLabel *label, bool value)
{
    if(value){
        label->setStyleSheet("border-radius:15px;border:1px solid black;background-color: rgb(0, 255, 0);");
    }else
    {
        label->setStyleSheet("border-radius:15px;border:1px solid black;background-color: rgb(255, 0, 0);");
    }
}

void dailyRecord::setLED_StationStatus(QLabel *label, int value)
{

    switch (value) {
    case 0:
        label->setStyleSheet("border-radius:15px;border:1px solid black;background-color: rgb(0, 255, 0);");
        break;
    case 1:
        label->setStyleSheet("border-radius:15px;border:1px solid black;background-color: rgb(255, 0, 0);");
        break;
    case 2:
        label->setStyleSheet("border-radius:15px;border:1px solid black;background-color: rgb(0, 0, 255);");
        break;
    case 3:
        label->setStyleSheet("border-radius:15px;border:1px solid black;background-color: rgb(255, 255, 127);");
        break;
    default:
        break;
    }

}

void dailyRecord::tableWidget_Show()
{


    ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection);                          //设置 tableWidget 不可选中
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);                        //设置 tableWidget 不可编辑
    ui->tableWidget->setFocusPolicy(Qt::NoFocus);                                               //设置 tableWidget 去除虚框
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(31);                               //行高
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);            //列宽根据全表格大小自适应填充
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0,QHeaderView::ResizeToContents); //指定列宽自适应

    //创建 tableWidget 列
    ui->tableWidget->setRowCount(currentStationInfoMap.size());

    //行计数
    int i=0;

    foreach(deviceI iter,currentStationInfoMap.values())
    {
        //列表序号
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
        ui->tableWidget->item(i, 0)->setTextAlignment(Qt::AlignCenter);

        //库位名称
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(iter.stationDesc));
        ui->tableWidget->item(i, 1)->setTextAlignment(Qt::AlignCenter);

        //库位描述
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(ESS_Single::GetInstance()->_StationNameDescMap.value(iter.stationDesc)));
        ui->tableWidget->item(i, 2)->setTextAlignment(Qt::AlignCenter);

        //库位状态
        QLabel *Label_LED = new QLabel();                       //定义一个 QLabel 标签类
        QWidget *widget = new QWidget;                          //定义一个 QWidget 窗口类
        QHBoxLayout *layout = new QHBoxLayout;                  //定义一个 QHBoxLayout 水平布局类

        Label_LED->setMaximumSize(30,30);                       //设置 QLabel 大小，比列表行小
        setLED_StationStatus(Label_LED, iter.stationStatus);    //设置 LED 颜色
        layout->addWidget(Label_LED);                           //设置 QLabel 水平布局，默认居中
        layout->setContentsMargins(0, 0, 0, 0);                 //设置 QLabel 边距
        widget->setLayout(layout);                              //添加进 QWidget
        ui->tableWidget->setCellWidget(i,3,widget);             //添加进 tableWidget 指定位置

        i++;
    }

    ui->label_count->setText(tr("库位数量：")+QString::number(i));

}

void dailyRecord::ON_recordTimer()
{
    if(qdebug_logstop){
        QString logtext=log_single::GetInstance()->getqDebug_log();
        if(logtext!=""){
            qdebug_logstopcount++;
            ui->qdebug_log->append(logtext);
        }
    }
    if(qdebug_logstopcount>1000){
        qdebug_logstopcount=0;
        ui->qdebug_log->clear();
    }
}

void dailyRecord::ON_setLedTimer()
{

    //获取 Modbus 状态信息
    _ModbusAllData = ESS_Single::GetInstance()->getModbusAllData();

    /********************* 清洗机入料 ******************/
    setLED(ui->label_74, _ModbusAllData.QR.connectStatus);
    setLED(ui->label_86, _ModbusAllData.QR.Y10);

    setLED(ui->label_88, _ModbusAllData.QR.X10);
    setLED(ui->label_78, _ModbusAllData.QR.X11);
    setLED(ui->label_80, _ModbusAllData.QR.X12);
    setLED(ui->label_142, _ModbusAllData.QR.X13);

    /********************* 清洗机出料 ******************/
    setLED(ui->label_90, _ModbusAllData.QC.connectStatus);
    setLED(ui->label_92, _ModbusAllData.QC.Y10);

    setLED(ui->label_94, _ModbusAllData.QC.X10);
    setLED(ui->label_96, _ModbusAllData.QC.X11);
    setLED(ui->label_98, _ModbusAllData.QC.X12);

    /********************* 人工上料位 ******************/
    setLED(ui->label_70, _ModbusAllData.RS.connectStatus);
    setLED(ui->label_82, _ModbusAllData.RS.Y10);
    setLED(ui->label_84, _ModbusAllData.RS.Y11);

    setLED(ui->label_72, _ModbusAllData.RS.X11);
    setLED(ui->label_76, _ModbusAllData.RS.X12);

    /********************* 人工下料位 ******************/
    setLED(ui->label_102, _ModbusAllData.RX.connectStatus);
    setLED(ui->label_104, _ModbusAllData.RX.Y10);
    setLED(ui->label_106, _ModbusAllData.RX.Y11);

    setLED(ui->label_110, _ModbusAllData.RX.X11);
    setLED(ui->label_114, _ModbusAllData.RX.X12);

}

void dailyRecord::ON_setLed_StationStatus_Timer()
{

    //实时查询库位状态信息
    currentStationInfoMap = threadQueryI->select_StationMap();

    //tableWidget 库位状态显示
    tableWidget_Show();

}

void dailyRecord::ON_Translate()
{
    qApp->installTranslator(&ESS_Single::GetInstance()->trans);
    ui->retranslateUi(this);
}

void dailyRecord::on_stop_Button_clicked()
{
    if(qdebug_logstop){
        qdebug_logstop=false;
        ui->stop_Button->setText(tr("暂停"));
    }else{
        qdebug_logstop=true;
        ui->stop_Button->setText(tr("启动"));
    }
}

void dailyRecord::on_clear_Button_clicked()
{
    ui->qdebug_log->clear();
}


void dailyRecord::on_loadPass_clicked()
{
    int _address = 0;

    //PLC对象
    int _Index = ui->taskStatusBox_object->currentIndex();

    //线圈地址
    int _Index_address = ui->taskStatusBox_address->currentIndex();

    //线圈写入值
    int value = ui->lineEdit_value->text().toInt();

    if(ui->lineEdit_value->text().isEmpty() || (value!=0 && value!=1)){
        //提示写入值空白或错误
        QMessageBox::warning(NULL, tr("警告"), tr("写入值为空或错误！！！"));
        return;
    }

    if(_Index == 0 && _Index_address == 0){
        _address = 999;
    }else if(_Index == 1 && _Index_address == 0){
        _address = 1047;
    }else if(_Index == 2 && _Index_address == 0){
        _address = 1095;
    }else if(_Index == 2 && _Index_address == 1){
        _address = 1096;
    }else if(_Index == 3 && _Index_address == 0){
        _address = 1143;
    }else if(_Index == 3 && _Index_address == 1){
        _address = 1144;
    }

    sig_WriteModbusI(_address, value);//PLC 监控触发 Modbus 写入

    //提示写入值操作成功
    QMessageBox::information(NULL, tr("提示"), tr("写入值操作成功！！！"));

    qDebug()<<"------------on_loadPass_clicked() address:"<<_address<<" value:"<<value;
}

void dailyRecord::on_loadPass_6_clicked()
{
    //库位状态
    int Station_Status = -1;

    //库位描述
    QString _Station = ui->taskStatusBox_Station->currentText();

    //状态描述
    int _Index = ui->taskStatusBox_Status->currentIndex();

    if(_Index == 0)
    {
        Station_Status = 0;

    }else if(_Index == 1)
    {
        Station_Status = 1;
    }

    if(queryServer::getInstance()->SetMessagebox(tr("是否确认修改库位状态？")))
    {
        emit sig_UpdateStation(_Station, Station_Status, "");
    }

}
